var vue = new Vue({
    el : '#app',
    data : {
        deptTreeData : [],
        treeProps: {
            children: 'children',
            label: 'deptName'
        },
        tableData: [],
        loading: false,

        deptFormVisible: false,
        formRead: false,
        deptForm: {
            parentId:0,
            deptName : '',
            remark : '',
        },
        deptFormRules: {
            deptName: [
                {required: true, message: '请填写部门名称'},
            ]
        },

        userTableData: [],
        userQueryForm:{
            current : 1,
            size  : 10,
            total : 0,
        },
        userForm: {
            loginName : '',
            nickName  :'',
            telephone : '',
            remark : '',
            deptId:'',
        },
        userFormRules: {
            loginName: [{required: true, message: '请输入登录名称'},],
            password: [{required: true, message: '请输入登录密码'},],
            nickName: [{required: true, message: '请输入用户名'},],
            telephone: [{required: true, message: '请输入手机号码'},],
            deptId: [{required: true, message: '请选择部门'},],
        },
        userFormVisible:false,
        selectDept:{
            id:0,
        },
        loginNameDisabled : true,

    },
    created : function(){
        this.loadAllDept();

    },
    methods : {
        loadAllDept: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/dept/list' ,
                success: function (result) {
                    if (result.code === 200) {
                        self.deptTreeData = result.body;

                        if(result.body.length > 0){
                            self.selectDept = result.body[0];
                            self.loadBranchDept();
                            self.loadAllUser();
                        }
                    }
                }
            })
        },
        loadBranchDept: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/dept/branch/'+ self.selectDept.id,
                success: function (result) {
                    if (result.code === 200) {
                        self.tableData = result.body;
                    }
                }
            })
        },
        saveDept : function(){
            this.deptForm.parentId = this.selectDept.id;

            var self = this;
            self.$refs['deptForm'].validate(function (valid) {
                if (!valid) return false;
                $.ajax({
                    url: constant.ROOT_SITE + '/system/dept/save',
                    data: JSON.stringify(self.deptForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.deptFormVisible = false;
                            self.loadAllDept();
                            self.loadBranchDept()
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },
        //删除部门
        deleteDept : function(dept){
            var self = this;

            this.$confirm('您确定用删除该部门吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:  constant.ROOT_SITE + '/system/dept/delete/'+dept.id,
                    success: function (result) {
                        if (result.code === 200 && result.body) {
                            self.$message({
                                type: 'success',
                                message: '删除成功！'
                            });
                            self.loadAllDept();
                            self.loadBranchDept()
                        }
                    }
                })
            });

        },

        updateUserBusinessStatus:function(row){
            var self = this;
            var form = {
                id : row.id,
                businessFlag:!row.businessFlag,
            }
            $.ajax({
                url: constant.ROOT_SITE + '/system/user/save',
                data: JSON.stringify(form),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '操作成功！'
                        });
                        row.businessFlag = !row.businessFlag;
                    } else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },

        saveUser : function(){
            var self = this;
            this.userForm.deptId = this.selectDept.id;

            self.$refs['userForm'].validate(function (valid) {
                if (!valid) return false;

                $.ajax({
                    url: constant.ROOT_SITE + '/system/user/save',
                    data: JSON.stringify(self.userForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.userFormVisible = false;
                            self.loadAllUser();
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },

        loadAllUser: function () {
            var self = this;
            this.userQueryForm.deptId = this.selectDept.id;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/user/list' ,
                data: JSON.stringify(self.userQueryForm),
                success: function (result) {
                    if (result.code === 200) {
                        self.userTableData = result.body;
                        self.userQueryForm.total = result.total;
                    }
                }
            })
        },
        //删除
        deleteUser : function(user){
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/user/delete/' + user.id,
                success: function (result) {
                    if (result.code === 200 && result.body) {
                        self.$message({
                            type: 'success',
                            message: '删除成功！'
                        });
                        self.loadAllUser();
                    }
                }
            })
        },

        showDeptDialog : function(){
            this.deptFormVisible = true;
        },
        showEditDeptDialog : function(dept){
            $.extend(this.deptForm, dept);
            this.deptFormVisible = true;
        },
        updateBusinessStatus:function(row){
            var self = this;
            var form = {
                id : row.id,
                businessFlag:!row.businessFlag,
            }
            $.ajax({
                url: constant.ROOT_SITE + '/system/dept/save',
                data: JSON.stringify(form),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '操作成功！'
                        });
                        row.businessFlag = !row.businessFlag;
                    } else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },
        handleNodeClick : function(dept){
            this.loadBranchDept();
            this.selectDept = dept;
            this.loadAllUser();
        },
        handleDialogClose :function () {
            this.deptForm={
                parentId:0,
                deptName : '',
                remark : '',
            };
            this.userForm={
                loginName : '',
                nickName  :'',
                telephone : '',
                remark : '',
                deptId :'',
            }
        },
        showUserDialog:function (user) {
            if(null == user){
                this.loginNameDisabled = false;
            }else {
                this.loginNameDisabled = true;
            }
            $.extend(this.userForm, user);

            this.userFormVisible = true;
        },
        handleSizeChange: function (pageSize) {
            this.userQueryForm.size = pageSize;
            this.loadAllUser();
        },
        handleCurrentChange: function (pageCurrent) {
            this.userQueryForm.current = pageCurrent;
            this.loadAllUser();
        },
    }
});